{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#firt project, gauess the code.\n",
    "import math\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Circle:\n",
    "    def __init__(self,radius):\n",
    "        self.radius = radius\n",
    "        \n",
    "    def get_area(self):\n",
    "        return math.pi * self.radius *self.radius\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A circle with radius 1 has area 3.14\n",
      "A circle with radius 3 has area 28.27\n",
      "A circle with radius 5 has area 78.54\n",
      "A circle with radius 7 has area 153.94\n",
      "A circle with radius 9 has area 254.47\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,10):\n",
    "    if (i & 1) ==0:\n",
    "        continue\n",
    "    circle = Circle(i)\n",
    "    print(\"A circle with radius {0} has area {1:0.2f}\".format(i,circle.get_area()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "#generate a guess\n",
    "geneSet = \" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!.\"\n",
    "target = \" For I am fearfully and wonderfully made.\"\n",
    "import random \n",
    "def generate_parent(length):\n",
    "    genes = []\n",
    "    while len(genes) < length:\n",
    "        sampleSize = min(length - len(genes),len(geneSet))#确定需要取样的长度\n",
    "        genes.extend(random.sample(geneSet,sampleSize))#扩展密码的数量\n",
    "    return ''.join(genes)\n",
    "    \n",
    "#min choose the lowest parameter\n",
    "# sample"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_fitness(guess):\n",
    "    return sum(1 for expected, actual in zip(target,guess)\n",
    "               if expected == actual)\n",
    "#Fitness value the genetics algorthim provides is the only feedback the engine gets to guide it to a solution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "#mutate to produce a new gauess\n",
    "def mutate(parent):\n",
    "    index = random.randrange(0,len(parent))\n",
    "    childGenes = list(parent)\n",
    "    newGene,alternate = random.sample(geneSet,2)\n",
    "    childGenes[index] = alternate \\\n",
    "        if newGene == childGenes[index] \\\n",
    "        else newGene\n",
    "    return ''.join(childGenes)\n",
    "'''if newGene ==childGenes[index]:\n",
    "    childGenes[index] = alternate\n",
    "else:\n",
    "    childGenes[index] = newGene\n",
    "    最后整合所有的结果\n",
    "    最后为了避免使用同样的一个猜测如果随机抽到相同的，避免重复计算\n",
    "'''    \n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "import datetime\n",
    "...\n",
    "def display(guess):\n",
    "    timeDiff = datetime.datetime.now() - startTime\n",
    "    fitness = get_fitness(guess)\n",
    "    print(\"{0}\\t{1}\\t{2}\".format(guess, fitness, str(timeDiff)))\n",
    "#having a visual representation  of genesquence, which may not be the literal gene sequence, ifs often critical to identifying what works and \n",
    "#and what not"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NLfMTqy.jeRYxUzJpiowvaXtCBlkmhrsScZgPGKnQ\t0\t0:00:00.001001\n"
     ]
    }
   ],
   "source": [
    "random.seed()\n",
    "startTime = datetime.datetime.now()\n",
    "bestParent =  generate_parent(len(target))\n",
    "bestFitness = get_fitness(bestParent)\n",
    "display(bestParent)\n",
    "\n",
    "\"\"\"\n",
    "看孩子的匹配度是不是高于父亲的匹配度，如果是该孩子成为下一代的父亲，否则舍弃掉\n",
    "\"\"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NLfMTqy.jeRYxUzJpiowvaXtCBlkmhruScZgPGKnQ\t1\t0:00:00.780616\n",
      "NLfMTqy.jeRYxUzJpiowvnXtCBlkmhruScZgPGKnQ\t2\t0:00:00.783617\n",
      "NLfM qy.jeRYxUzJpiowvnXtCBlkmhruScZgPGKnQ\t3\t0:00:00.783617\n",
      "NLfM qy.jeRYxUzJpiowvnXtCBlkmhrulcZgPGKnQ\t4\t0:00:00.784619\n",
      "NLfM qy.jeRYxUzJplowvnXtCBlkmhrulcZgPGKnQ\t5\t0:00:00.786618\n",
      "NLfM qy.jeRYxrzJplowvnXtCBlkmhrulcZgPGKnQ\t6\t0:00:00.787618\n",
      "NLfr qy.jeRYxrzJplowvnXtCBlkmhrulcZgPGKnQ\t7\t0:00:00.787618\n",
      "NLfr qy.meRYxrzJplowvnXtCBlkmhrulcZgPGKnQ\t8\t0:00:00.788634\n",
      "NLfr qy.meRYxrzJplowvnXtCBlkmhrulcZgPGKn.\t9\t0:00:00.788634\n",
      "NLfr qy.meRYxrzJplowvnXtCBlkmhrulcZgPGKe.\t10\t0:00:00.788634\n",
      "NLfr qy.meRYxrzJplowvnXtCBlkmhrullZgPGKe.\t11\t0:00:00.789620\n",
      "NLfr qy.meRYxrzJplowvnXtCBnkmhrullZgPGKe.\t12\t0:00:00.790618\n",
      "NLfr qy.meRYxrzJplo vnXtCBnkmhrullZgPGKe.\t13\t0:00:00.791621\n",
      "NLfr qy.meRYxrzuplo vnXtCBnkmhrullZgPGKe.\t14\t0:00:00.791621\n",
      "NLfr qy.meRYxrzuplo anXtCBnkmhrullZgPGKe.\t15\t0:00:00.791621\n",
      "NLfr qy.meRYxrzuplo anXtCBnkmhrullZgmGKe.\t16\t0:00:00.792618\n",
      "NLor qy.meRYxrzuplo anXtCBnkmhrullZgmGKe.\t17\t0:00:00.793617\n",
      "NLor qy.meRYxrzuplo anXtwBnkmhrullZgmGKe.\t18\t0:00:00.797618\n",
      "NLor qy.meRYxrzullo anXtwBnkmhrullZgmGKe.\t19\t0:00:00.797618\n",
      "NLor qy.meRYxrzullo anX wBnkmhrullZgmGKe.\t20\t0:00:00.798618\n",
      "NLor qy.meRYxrzullo anX wBnkmhrullZgmGde.\t21\t0:00:00.799617\n",
      "NLor qy.meRYarzullo anX wBnkmhrullZgmGde.\t22\t0:00:00.801619\n",
      "NLor qy.meRYarzullo anX wonkmhrullZgmGde.\t23\t0:00:00.802618\n",
      "NLor qy.meRearzullo anX wonkmhrullZgmGde.\t24\t0:00:00.802618\n",
      "NLor qy.meRearzullo anX wondmhrullZgmGde.\t25\t0:00:00.803619\n",
      "NLor qy.meRearzullo anX wondmhrullZ mGde.\t26\t0:00:00.807619\n",
      " Lor qy.meRearzullo anX wondmhrullZ mGde.\t27\t0:00:00.808618\n",
      " For qy.meRearzullo anX wondmhrullZ mGde.\t28\t0:00:00.808618\n",
      " For qy.meRearzullo anX wondmhrullZ made.\t29\t0:00:00.809619\n",
      " For qy.meRearzullo and wondmhrullZ made.\t30\t0:00:00.811619\n",
      " For qy.m Rearzullo and wondmhrullZ made.\t31\t0:00:00.812619\n",
      " For qy.m Rearzullo and wondmhfullZ made.\t32\t0:00:00.813619\n",
      " For Iy.m Rearzullo and wondmhfullZ made.\t33\t0:00:00.817620\n",
      " For Iy.m Rearzully and wondmhfullZ made.\t34\t0:00:00.821620\n",
      " For Iyam Rearzully and wondmhfullZ made.\t35\t0:00:00.825620\n",
      " For I am Rearzully and wondmhfullZ made.\t36\t0:00:00.826620\n",
      " For I am Rearzully and wondehfullZ made.\t37\t0:00:00.845622\n",
      " For I am Rearzully and wonderfullZ made.\t38\t0:00:00.845622\n",
      " For I am Rearzully and wonderfully made.\t39\t0:00:00.850622\n",
      " For I am fearzully and wonderfully made.\t40\t0:00:00.850622\n",
      " For I am fearfully and wonderfully made.\t41\t0:00:00.867623\n"
     ]
    }
   ],
   "source": [
    "while True:\n",
    "    child = mutate(bestParent)\n",
    "    childFitness = get_fitness(child)\n",
    "    if bestFitness >= childFitness:\n",
    "        continue\n",
    "    display(child)\n",
    "    if childFitness >= len(bestParent):\n",
    "        break\n",
    "    bestFitness = childFitness\n",
    "    bestParent = child"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
